home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************************/
- /* */
- /* printf.c - Display diagnostic/informative messages */
- /* */
- /* Richard W. Mincher. 5/1/87. */
- /* Philip Rakity 8/27/87 */
- /* */
- /* Copyright © 1987-88, Apple Computer, Inc. All rights reserved. */
- /* */
- /************************************************************************************/
-
- #include "os.h"
- #include "managers.h"
-
- pascal void illegal ()
- extern 0x4afc;
-
- static char pr_object_name [] = "print manager";
- static char pr_type_name [] = "print manager";
- #define line_length 257
-
- void
- printf( string, arg )
- char *string;
- int arg;
- {
- void print_guts();
- tid_type GetPrintTID ();
- static tid_type print_task;
- long count;
- message *msg, *m;
- char *buffer;
- long i;
- unsigned long mid;
-
- for (i = 0; (print_task == 0) && (i < 30); i++) /* Try to find a print manager */
- {
- print_task = GetPrintTID ();
- if (print_task == 0)
- m = Receive (OS_MATCH_NONE, OS_MATCH_NONE, OS_MATCH_NONE, GetTickPS ());
- }
-
- if (print_task == 0)
- return;
-
- buffer = (char *)GetMem( line_length );
- if (buffer)
- {
- msg = GetMsg();
- if (msg)
- {
- print_guts(string, &arg, buffer);
- for( count = 0; buffer[count]; count++ )
- ;
-
- msg->mDataSize = count;
- msg->mDataPtr = buffer;
-
- msg -> mTo = print_task;
-
- msg->mCode = PRINT_ME;
- mid = msg->mId;
- Send( msg );
- msg = Receive( mid, OS_MATCH_ALL, OS_MATCH_ALL, 30*GetTickPS() );
- if (msg != NULL)
- {
- if (msg -> mCode != (PRINT_ME+1))
- {
- print_task = 0; /* We failed to print for some reason */
- }
- FreeMem( msg->mDataPtr );
- FreeMsg( msg );
- }
- else
- {
- print_task = 0; /* We failed to print for some reason */
- }
- }
- else
- {
- FreeMem( buffer );
- }
- }
- }
-
- /*
- * Common code for printf et al.
- *
- * The calling routine typically takes a variable number of arguments,
- * and passes the address of the first one. This implementation
- * assumes a straightforward, stack implementation, aligned to the
- * machine's wordsize. Increasing addresses are assumed to point to
- * successive arguments (left-to-right), as is the case for a machine
- * with a downward-growing stack with arguments pushed right-to-left.
- *
- * To write, for example, fprintf() using this routine, the code
- *
- * fprintf(fd, format, args)
- * FILE *fd;
- * char *format;
- * {
- * _doprnt(format, &args, fd);
- * }
- *
- * would suffice. (This example does not handle the fprintf's "return
- * value" correctly, but who looks at the return value of fprintf
- * anyway?)
- *
- * This version implements the following printf features:
- *
- * %d decimal conversion
- * %u unsigned conversion
- * %x hexadecimal conversion
- * %X hexadecimal conversion with capital letters
- * %o octal conversion
- * %c character
- * %s string
- * %m.n field width, precision
- * %-m.n left adjustment
- * %0m.n zero-padding
- * %*.* width and precision taken from arguments
- *
- * This version does not implement %f, %e, or %g. It accepts, but
- * ignores, an `l' as in %ld, %lo, %lx, and %lu, and therefore will not
- * work correctly on machines for which sizeof(long) != sizeof(int).
- * It does not even parse %D, %O, or %U; you should be using %ld, %o and
- * %lu if you mean long conversion.
- *
- * This version implements the following nonstandard features:
- *
- * %b binary conversion
- * %r roman numeral conversion
- * %R roman numeral conversion with capital letters
- *
- * As mentioned, this version does not return any reasonable value.
- *
- * Permission is granted to use, modify, or propagate this code as
- * long as this notice is incorporated.
- *
- * Steve Summit 3/25/87
- */
-
- #define TRUE 1
- #define FALSE 0
-
- #define ROMAN
-
- #define isdigit(d) ((d) >= '0' && (d) <= '9')
- #define Ctod(c) ((c) - '0')
- #define stout(v) \
- if(line_length > (length_of_line+=1)) \
- { \
- *(out++) = (v); \
- } \
- else \
- { \
- *(out++) = 0; \
- return; \
- }
-
- #define MAXBUF (sizeof(long int) * 8) /* enough for binary */
-
- #ifdef ROMAN
- static tack();
- static doit();
- #endif
-
- static void
- print_guts(fmt, argp, out)
- register char *fmt;
- register int *argp;
- register char *out;
- {
- register char *p;
- char *p2;
- int size;
- int length;
- int prec;
- int ladjust;
- char padc;
- int n;
- unsigned int u;
- int base;
- char buf[MAXBUF];
- int negflag;
- char *digs;
- short length_of_line;
- #ifdef ROMAN
- char *rdigs;
- int d;
- #endif
-
- length_of_line = 0;
-
- while(*fmt != '\0')
- {
- if(*fmt != '%')
- {
- stout(*fmt++);
- continue;
- }
-
- fmt++;
-
- if(*fmt == 'l')
- fmt++; /* need to use it if sizeof(int) < sizeof(long) */
-
- length = 0;
- prec = -1;
- ladjust = FALSE;
- padc = ' ';
-
- if(*fmt == '-')
- {
- ladjust = TRUE;
- fmt++;
- }
-
- if(*fmt == '0')
- {
- padc = '0';
- fmt++;
- }
-
- if(isdigit(*fmt))
- {
- while(isdigit(*fmt))
- length = 10 * length + Ctod(*fmt++);
- }
- else if(*fmt == '*')
- {
- length = *argp++;
- fmt++;
- if(length < 0)
- {
- ladjust = !ladjust;
- length = -length;
- }
- }
-
- if(*fmt == '.')
- {
- fmt++;
- if(isdigit(*fmt))
- {
- prec = 0;
- while(isdigit(*fmt))
- prec = 10 * prec + Ctod(*fmt++);
- }
- else if(*fmt == '*')
- {
- prec = *argp++;
- fmt++;
- }
- }
-
- negflag = FALSE;
- digs = "0123456789abcdef";
- #ifdef ROMAN
- rdigs = " mdclxvi";
- #endif
-
- switch(*fmt)
- {
- case 'b':
- case 'B':
- u = *argp++;
- base = 2;
- goto donum;
-
- case 'c':
- stout(*argp++);
- break;
-
- case 'd':
- case 'D':
- n = *argp++;
-
- if(n >= 0)
- u = n;
- else {
- u = -n;
- negflag = TRUE;
- }
-
- base = 10;
-
- goto donum;
-
- case 'o':
- case 'O':
- u = *argp++;
- base = 8;
- goto donum;
- #ifdef ROMAN
- case 'R':
- rdigs = " MDCLXVI";
- case 'r':
- n = *argp++;
- p2 = &buf[MAXBUF - 1];
-
- d = n % 10;
- tack(d, &rdigs[6], &p2);
- n = n / 10;
-
- d = n % 10;
- tack(d, &rdigs[4], &p2);
- n = n / 10;
-
- d = n % 10;
- tack(d, &rdigs[2], &p2);
- n /= 10;
-
- d = n % 10;
- tack(d, rdigs, &p2);
-
- p = p2;
-
- goto putpad;
- #endif
- case 's':
- p = (char *)(*argp++);
-
- if(p == 0)
- p = "(NIL)";
-
- if(length > 0 && !ladjust)
- {
- n = 0;
- p2 = p;
-
- for(; *p != '\0' &&
- (prec == -1 || n < prec); p++)
- n++;
-
- p = p2;
-
- while(n < length)
- {
- stout(' ');
- n++;
- }
- }
-
- n = 0;
-
- while(*p != '\0')
- {
- if(++n > prec && prec != -1)
- break;
-
- stout(*p++);
- }
-
- if(n < length && ladjust)
- {
- while(n < length)
- {
- stout(' ');
- n++;
- }
- }
-
- break;
-
- case 'u':
- case 'U':
- u = *argp++;
- base = 10;
- goto donum;
-
- case 'X':
- digs = "0123456789ABCDEF";
- case 'x':
- u = *argp++;
- base = 16;
-
- donum: p = &buf[MAXBUF - 1];
-
- do {
- *p-- = digs[u % base];
- u /= base;
- } while(u != 0);
-
- if(negflag)
- stout('-');
- putpad:
- size = &buf[MAXBUF - 1] - p;
-
- if(size < length && !ladjust)
- {
- while(length > size)
- {
- stout(padc);
- length--;
- }
- }
-
- while(++p != &buf[MAXBUF])
- stout(*p);
-
- if(size < length) /* must be ladjust */
- {
- while(length > size)
- {
- stout(padc);
- length--;
- }
- }
-
- break;
-
- case '\0':
- fmt--;
- break;
-
- default:
- stout(*fmt);
- }
- fmt++;
- }
- stout(0);
- }
-
- #ifdef ROMAN
-
- static
- tack(d, digs, p)
- int d;
- char *digs;
- char **p;
- {
- if(d == 0) return;
- if(d >= 1 && d <= 3)
- {
- doit(d, digs[2], p);
- return;
- }
-
- if(d == 4 || d == 5)
- {
- **p = digs[1];
- (*p)--;
- }
-
- if(d == 4)
- {
- **p = digs[2];
- (*p)--;
- return;
- }
-
- if(d == 5) return;
-
- if(d >= 6 && d <= 8)
- {
- doit(d - 5, digs[2], p);
- **p = digs[1];
- (*p)--;
- return;
- }
-
- /* d == 9 */
-
- **p = digs[0];
- (*p)--;
- **p = digs[2];
- (*p)--;
- return;
- }
-
- static
- doit(d, one, p)
- int d;
- char one;
- char **p;
- {
- int i;
-
- for(i = 0; i < d; i++)
- {
- **p = one;
- (*p)--;
- }
- }
-
- #endif
-
- static tid_type GetPrintTID ()
- {
- tid_type printTID;
- struct ra_GetCards get_cards;
- message *msgptr;
- unsigned short index;
- short s;
-
- tid_type GetIMMPrintTID();
-
- printTID = 0;
-
- if (GetICCTID () != 0)
- {
- if ((msgptr = GetMsg ()) == NULL)
- return (printTID);
-
- msgptr -> mCode = ICC_GETCARDS;
- msgptr -> mDataPtr = (char *) &get_cards;
- msgptr -> mDataSize = sizeof (struct ra_GetCards);
- msgptr -> mTo = GetICCTID ();
- Send (msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, ICC_GETCARDS+1, OS_NO_TIMEOUT);
-
- if (msgptr -> mStatus == 0)
- {
- for (s = 0; (s < IC_MAXCARDS) && (printTID == 0); s++)
- {
- if (get_cards.tid [s] > 0)
- {
- index = 0;
- printTID = Lookup_Task (pr_object_name, pr_type_name,
- get_cards.tid [s], &index);
- }
- }
- }
- FreeMsg (msgptr);
- }
- else
- {
- index = 0;
- printTID = Lookup_Task (pr_object_name, pr_type_name, GetNameTID (), &index);
- }
-
- if (printTID == 0)
- {
- printTID = GetIMMPrintTID();
- }
-
- return (printTID);
- }
-
- static tid_type GetIMMPrintTID ()
- {
- tid_type printTID;
- struct ra_GetCards get_cards;
- struct ra_GetIMMs get_imms;
- message *msgptr;
- short i;
- short immcount;
- unsigned short index;
- short s;
- short ss;
- struct LookupIMMQ LookupIMMQbuf[40];
- tid_type immTID;
-
- printTID = 0;
-
- if (GetICCTID () != 0)
- {
- if ((msgptr = GetMsg ()) == NULL)
- return (printTID);
-
- msgptr -> mCode = ICC_GETIMMS;
- msgptr -> mDataPtr = (char *) &get_imms;
- msgptr -> mDataSize = sizeof (get_imms);
- msgptr -> mTo = GetICCTID ();
- Send (msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, ICC_GETIMMS+1, OS_NO_TIMEOUT);
-
- if (msgptr -> mStatus == 0)
- {
- for (s = 0; (s < IC_MAXCARDS) && (printTID == 0); s++)
- {
- if (get_imms.tid [s] > 0)
- {
- LookupIMMQbuf[0].objectlen = 1;
- LookupIMMQbuf[0].object[0] = '=';
- LookupIMMQbuf[0].zonelen = 1;
- LookupIMMQbuf[0].zone[0] = '*';
- msgptr -> mFrom = GetTID();
- msgptr -> mTo = get_imms.tid[s];
- msgptr -> mDataPtr = (char *) LookupIMMQbuf;
- msgptr -> mDataSize = sizeof (LookupIMMQbuf);
- msgptr -> mCode = IMM_LookupIMM;
- msgptr -> mOData[0] = 8; /* retry time interval and count for NBP */
- msgptr -> mOData[1] = 1;
- Send(msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, IMM_LookupIMM+1, OS_NO_TIMEOUT);
-
- immcount = msgptr -> mOData[2];
-
- for (i = 0; (i < immcount) && (printTID == 0); i++)
- {
- msgptr -> mFrom = GetTID();
- msgptr -> mTo = get_imms.tid[s];
- BlockMove(&LookupIMMQbuf[i].address[0],
- &msgptr -> mOData[0], IM_Address_Size);
- msgptr -> mCode = IMM_MapIMM;
- Send(msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, IMM_MapIMM+1, OS_NO_TIMEOUT);
- immTID = (tid_type) msgptr -> mOData[0];
-
- msgptr -> mFrom = GetTID();
- msgptr -> mCode = ICC_GETCARDS;
- msgptr -> mDataPtr = (char *) &get_cards;
- msgptr -> mDataSize = sizeof (get_cards);
- msgptr -> mTo = immTID;
- Send (msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, ICC_GETCARDS+1, OS_NO_TIMEOUT);
-
- if (msgptr -> mStatus == 0)
- {
- for (ss = 0; (ss < IC_MAXCARDS) && (printTID == 0); ss++)
- {
- if (get_cards.tid [ss] > 0)
- {
- msgptr -> mFrom = GetTID();
- msgptr -> mTo = get_imms.tid[s];
- msgptr -> mOData[0] = immTID;
- msgptr -> mOData[1] = get_cards.tid [ss];
- msgptr -> mCode = IMM_MapTID;
- Send(msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, IMM_MapTID+1, OS_NO_TIMEOUT);
-
- if (msgptr -> mOData[2] != 0)
- {
- index = 0;
- printTID = Lookup_Task (pr_object_name, pr_type_name,
- msgptr -> mOData[2], &index);
- }
-
- if (printTID != 0)
- {
- msgptr -> mFrom = GetTID();
- msgptr -> mTo = get_imms.tid[s];
- msgptr -> mOData[0] = msgptr -> mOData[2];
- msgptr -> mOData[1] = printTID;
- msgptr -> mCode = IMM_MapTID;
- Send(msgptr);
-
- msgptr = Receive (OS_MATCH_ALL, OS_MATCH_ALL, IMM_MapTID+1, OS_NO_TIMEOUT);
- printTID = msgptr -> mOData[2];
- }
- }
- }
- }
- }
- }
- }
- }
- FreeMsg (msgptr);
- }
- return (printTID);
- }
-